<template>
	<view class="goods-item" >
		<view class="left">
			<radio @click="changeStatus" v-if="showRadio" :checked="item.goods_status" color="#c00000"></radio>
			<image :src="item.goods_small_logo || defaultImg" mode=""></image>
		</view>
		<view class="right">
			<view class="title" @click="toDetail(item.goods_id)">
				{{ item.goods_name }}
			</view>
			<view class="price">
				<text>￥<text class="pricenum">{{ item.goods_price | tofixed }}</text></text>
				<uni-number-box :min="1" @change="myChange" v-if="showNum" v-model="item.goods_count"></uni-number-box>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "my-goods",
		// item：当前商品的数据源
		// showRadio：是否显示单选框
		props: ['item', 'showRadio', 'showNum'],
		data() {
			return {
				// 定义一张默认图片
				defaultImg: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
			};
		},
		filters: {
			tofixed(num) {
				return num.toFixed(2)
			}
		},
		methods: {
			changeStatus() {
				// 调用父组件中的方法：radio-change
				this.$emit('radio-change', {
					goods_id: this.item.goods_id,
					goods_status: !this.item.goods_status
				})
			},
			myChange(e) {
				this.$emit('num-change', {
					goods_id: this.item.goods_id,
					goods_count: e
				})
			},
			toDetail(id) {
				uni.navigateTo({
					url: `/pages/goods_detail/main?goods_id=${id}`
				})
			},
		}
	}
</script>

<style lang="scss">
	.goods-item {
		padding-bottom: 100rpx;
		
		display: flex;
		padding: 30rpx 0;
		border-bottom: 1px solid #ccc;

		.left {
			display: flex;
			align-items: center;

			radio {
				margin: 0 10rpx;
			}

			image {
				width: 260rpx;
				height: 260rpx;
				margin-right: 20rpx;
			}
		}

		.right {
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			margin-right: 20rpx;

			.title {
				font-size: 16px;
				display: -webkit-box;
				text-overflow: ellipsis;
				overflow: hidden;
				-webkit-line-clamp: 2; // 设置两行文字溢出
				-webkit-box-orient: vertical;
			}

			.price {
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 14px;
				color: #eb4450;
				.pricenum {
					font-size: 18px;
				}
			}
		}
	}
</style>
